<?php

class Admin_Population_Controller extends Base_Controller {

	public function action_index()
	{
		return View::make('admin.population');
	}

	public function action_getinfo()
	{
		$admininfo = DB::table('population') -> paginate(15);
		foreach($admininfo->results as $key=>$val){
			$admininfo->results[$key]['popula'] = ($val['popula'])*100;
		}
        return json_encode(array('code'=>0,'msg'=>'','count'=> $admininfo->total,'data'=>$admininfo->results));

	}

	public function action_change()
	{
		$id = Input::get('id');
		$populationinfo = DB::table('population')->where('id','=',$id)->first();

		return View::make('admin.populationchange',array('populationinfo'=>$populationinfo));

	}

	public function action_dochange()
	{

		$id = Input::get('id');
		$population = Input::get('population')/100;
		$populares = DB::table('population')->where('id','=',$id)->update(array('popula'=>$population));
		if($populares){
			return json_encode(array('code'=>200,'msg'=>'修改成功'));
		}else{
			return json_encode(array('code'=>400,'msg'=>'修改失败'));
		}

	}


	/*
	判断是否中奖的接口;返回json数据 
	{
		"code":200,
		"errmsg":"恭喜中奖",
	}
	{
		"code":400,
		"errmsg":"很遗憾",
	}
	*/
	public function action_isprize()
	{
		$popula = DB::table('population')->first();
		$population = $popula['popula'];  //0.009
		$intnumber = explode(".",$population)[1];  //009
		$mun =  strlen($intnumber); //获取小数点后的位数 3
		$munber = rand(0,pow(10,$mun)); // (0-1000)
		if($munber<=$intnumber){
			return json_encode(array('msg'=>'恭喜中奖','code'=>200));
		}else{
			return json_encode(array('msg'=>'很遗憾','code'=>400));
		}
	}








}

